﻿using EFCore.Models;
using Microsoft.EntityFrameworkCore;
using SIService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SService
{
    public class PurchasereturnS : PurchasereturnSI
    {
        private readonly WmssystemContext myDb;

        public PurchasereturnS(WmssystemContext myDb)
        {
            this.myDb = myDb;
        }

        public Task<int> Add(PO_Purchasereturn pO_Purchasereturn)
        {
            myDb.PO_Purchasereturn.Add(pO_Purchasereturn);
            return myDb.SaveChangesAsync();
        }

        public Task<List<PO_Purchasereturn>> Fye(int pageSize, int currentPage, string Number = null, string Status = null, string Supplier = null, DateTime? date = null, DateTime? date1 = null)
        {
            var query = from allot in myDb.PO_Purchasereturn
                            //join Support in myDb.Su_Suppliermanagement on allot.P_RSupplier equals Support.S_MIndex.ToString()
                        select new PO_Purchasereturn
                        {
                            P_RIndex = allot.P_RIndex,
                            P_RReturnOrder = allot.P_RReturnOrder,
                            P_RStatus = allot.P_RStatus,
                            P_RReturnDate = allot.P_RReturnDate,
                            P_RPurchaseOrder = allot.P_RPurchaseOrder,
                            P_RSupplier = allot.P_RSupplier,
                            P_RContactPerson = allot.P_RContactPerson,
                            P_RContactInformation = allot.P_RContactInformation,
                            P_RPurchasingDepartment = allot.P_RPurchasingDepartment,
                            P_RPurchaser = allot.P_RPurchaser,
                            P_RReturnQuantity = allot.P_RReturnQuantity,
                            P_RReturnAmount = allot.P_RReturnAmount,
                            P_RReturnedItems = allot.P_RReturnedItems,
                            P_RCreator = allot.P_RCreator,
                            P_RDepartment = allot.P_RDepartment,
                            P_RCreationTime = allot.P_RCreationTime,
                            P_RApprover = allot.P_RApprover,
                            P_RApproverTime = allot.P_RApproverTime,
                            P_Reamr = allot.P_Reamr,
                            P_Reviewcomments = allot.P_Reviewcomments
                        };

            // 根据条件进行查询
            if (!string.IsNullOrEmpty(Number))
            {
                query = query.Where(c => c.P_RReturnOrder.Contains(Number));
            }
            if (!string.IsNullOrEmpty(Status))
            {
                query = query.Where(c => c.P_RStatus == Status);
            }
            if (!string.IsNullOrEmpty(Supplier))
            {
                query = query.Where(c => c.P_RSupplier == Supplier);
            }
            if (date != null)
            {
                query = query.Where(e => e.P_RReturnDate >= date);
            }
            if (date1 != null)
            {
                query = query.Where(e => e.P_RReturnDate <= date1);
            }

            // 分页查询
            return query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToListAsync();
        }

        public Task<List<PO_Purchasereturn>> Qu()
        {
            return myDb.PO_Purchasereturn.ToListAsync();
        }

        public async Task<int> ThDel(List<int> Ids)
        {
            // 使用 LINQ 查询语句检索相应的记录
            var detailsToDelete = await myDb.PO_Purchasereturn
                .Where(d => Ids.Contains(d.P_RIndex))
                .ToListAsync();

            // 删除查询结果
            myDb.PO_Purchasereturn.RemoveRange(detailsToDelete);

            // 保存删除操作到数据库
            return await myDb.SaveChangesAsync();
        }

        public async Task<int> ThShen(PO_Purchasereturn pO_Purchasereturn, string to)
        {
            // 1. 根据指定条件查找要审核的记录
            var purchaseOrder = await myDb.PO_Purchasereturn.FindAsync(pO_Purchasereturn.P_RIndex);

            if (purchaseOrder == null)
            {
                // 如果没有找到对应的记录，可以进行适当的处理，比如抛出异常或者返回错误码
                throw new Exception("指定的记录不存在");
            }
            if (to == "通过")
            {
                purchaseOrder.P_RStatus = "已审核";
                //把货品退货的数量回写到进货明细表中
                var ppList = await myDb.PO_Purchasereturndetail.Where(c => c.P_UPurCharCode == purchaseOrder.P_RReturnOrder).ToListAsync();
                foreach (var pp in ppList)
                {
                    var ccList = await myDb.PO_Purchasedetails.Where(c => c.P_DProductCode == pp.P_UProductNumber).ToListAsync();
                    foreach (var cc in ccList)
                    {
                        cc.P_DQuantity = (int?)(pp.P_UPurchaseQuantity - pp.P_UReturnQuantity);
                        // 如果需要更新其他属性，也可以在这里进行修改
                    }
                }
            }
            else
            {
                purchaseOrder.P_RStatus = "待审核";
            }
            // 2. 更新找到的记录的相应字段
            purchaseOrder.P_RApprover = "admin"; // 以此类推，更新需要修改的字段
            purchaseOrder.P_RApproverTime = DateTime.Now;
            purchaseOrder.P_Reviewcomments = pO_Purchasereturn.P_Reviewcomments;


            // 3. 将更新后的记录保存回数据库中
            myDb.PO_Purchasereturn.Update(purchaseOrder);
            return await myDb.SaveChangesAsync(); // 返回受影响的行数
        }
    }
}
